#include"user.h"
#include<p33EP256GM710.h>
#include"Timer.h"


tagTimer Timer ;           //????????
static unsigned char Time_500mS = 0;
static unsigned char Time_1S = 0;



void Timer1Init(void)         //FCY = 64MHz   100MS
{
    T1CONbits.TON = 0;         //Disable Timer
    T1CONbits.TCS = 0;         //internal instruction cycle clock
    T1CONbits.TGATE = 0;       //Disable Gated Timer mode
    T1CONbits.TCKPS = 3;       //Select 1:256 Prescaler
    TMR1 = 0x0000;             //Clear timer register
    PR1  = 0x61A8;             //Load the period value
    IPC0bits.T1IP = 0x05;      //Interrupt Priority Level
    IFS0bits.T1IF = 0;         //Clear Timer Interrupt Flag
    IEC0bits.T1IE = 1;         //Enable Timer interrupt
    T1CONbits.TON = 1;         //Start Timer
}

void Timer2Init(void)          //FCY = 64MHz 50mS
{
    T2CONbits.TON = 0;          //Disable Timer
    T2CONbits.TCS = 0;          //internal instruction cycle clock
    T2CONbits.TGATE = 0;        //Disable Gated Timer mode
    T2CONbits.T32 = 0;          //16bit timer mode
    T2CONbits.TCKPS = 3;        //Select 1:256 Prescaler
    TMR2 = 0x0000;              //Clear timer register
    PR2  = 0x30D4;              //Load the period value
    IPC1bits.T2IP = 0x03;       //Interrupt Priority Level
    IFS0bits.T2IF = 0;          //Clear Timer Interrupt Flag
    IEC0bits.T2IE = 1;          //Enable Timer interrupt
    T2CONbits.TON = 1;          //Start Timer
}

//-------------------------------------------------------------------------------
// ????: void Timer3Init(void)
// ????: TMR3 ????   FCY = 64MHz 10mS
// ????: ?
// ????: ?
// ????: ?
// ?????
// ?????
//-------------------------------------------------------------------------------
void Timer3Init(void)          //FCY = 64MHz 10mS
{
    T3CONbits.TON = 0;          //Disable Timer
    T3CONbits.TCS = 0;          //internal instruction cycle clock
    T3CONbits.TGATE = 0;        //Disable Gated Timer mode
    T3CONbits.TCKPS = 2;        //Select 1:64  Prescaler
    TMR3 = 0x0000;              //Clear timer register
    PR3  = 0x2710;              //Load the period value
    IPC2bits.T3IP = 0x03;       //Interrupt Priority Level
    IFS0bits.T3IF = 0;          //Clear Timer Interrupt Flag
    IEC0bits.T3IE = 1;          //Enable Timer interrupt
    T3CONbits.TON = 1;          //Start Timer
}

//-------------------------------------------------------------------------------
// ????: void Timer4Init(void)
// ????: TMR4 ????     FCY = 64MHz 1mS
// ????: ?
// ????: ?
// ????: ?
// ?????
// ?????
//-------------------------------------------------------------------------------
void Timer4Init(void)          //FCY = 64MHz 1mS
{
    T4CONbits.TON = 0;          //Disable Timer
    T4CONbits.TCS = 0;          //internal instruction cycle clock
    T4CONbits.TGATE = 0;        //Disable Gated Timer mode
    T4CONbits.TCKPS = 1;        //Select 1:8 Prescaler
    TMR4 = 0x0000;              //Clear timer register
    PR4  = 0x1F40;              //Load the period value
    IPC6bits.T4IP = 0x03;       //Interrupt Priority Level
    IFS1bits.T4IF = 0;          //Clear Timer Interrupt Flag
    IEC1bits.T4IE = 1;          //Enable Timer interrupt
    T4CONbits.TON = 1;          //Start Timer
}

//-------------------------------------------------------------------------------
// ????: void Timer5Init(void)
// ????: TMR5 ????     FCY = 64MHz 1mS
// ????: ?
// ????: ?
// ????: ?
// ?????
// ?????
//-------------------------------------------------------------------------------
void Timer5Init(void)          //FCY = 64MHz 1mS
{
    T5CONbits.TON = 0;          //Disable Timer
    T5CONbits.TCS = 0;          //internal instruction cycle clock
    T5CONbits.TGATE = 0;        //Disable Gated Timer mode
    T5CONbits.TCKPS = 1;        //Select 1:8 Prescaler
    TMR5 = 0x0000;              //Clear timer register
    PR5  = 0x1F40;              //Load the period value
    IPC7bits.T5IP = 0x03;       //Interrupt Priority Level
    IFS1bits.T5IF = 0;          //Clear Timer Interrupt Flag
    IEC1bits.T5IE = 1;          //Enable Timer interrupt
    T5CONbits.TON = 1;          //Start Timer
}
//-------------------------------------------------------------------------------
// ????: T1Interrupt
// ????: TMR1 ????       FCY = 64MHZ   100MS
// ????: ?
// ????: ?
// ????: ?
// ?????
// ?????
//-------------------------------------------------------------------------------
void __attribute__((__interrupt__, no_auto_psv)) _T1Interrupt(void)
{	
    IFS0bits.T1IF = 0;     //????
    Timer.Flag_100mS = 1;  //????

    //ReaderTimer_1ms();

    Time_500mS ++;
    Time_1S ++;
    if(Time_500mS == 5)          //500mS????
    {
	    Time_500mS = 0;
	    Timer.Flag_500mS = 1;    //500mS????
    }
    if(Time_1S == 10)            //1S????
    {
	    Time_1S = 0;
	    Timer.Flag_1S = 1;       //1S????
            if(SysRunLED==1){SysRunLED=0;}
            else{SysRunLED=1;}
//           if(LATBbits.LATB13==1){LATBbits.LATB13=0;}
//           else {LATBbits.LATB13=1;}
    }

}

//-------------------------------------------------------------------------------
// ????: T3Interrupt
// ????: TMR3 ????  FCY = 64MHZ 10mS
// ????: ?
// ????: ?
// ????: ?
// ?????
// ?????
//-------------------------------------------------------------------------------
void __attribute__((__interrupt__, no_auto_psv)) _T3Interrupt(void)
{
    IFS0bits.T3IF = 0;    
    Timer.Flag_10mS = 1;  
    //-----
//    SoftTimeCount();      
}
